package com.xiaoyun.xpath.handler.field;

import cn.wanghaomiao.xpath.model.JXDocument;
import com.xiaoyun.xpath.HandlerUtils;

import java.lang.reflect.Method;
import java.util.List;

public class IntegerFieldHandler<T> extends StringFieldHandler<T> {
    @Override
    protected <T> void stringMethodInvoke(Class<T> clazz, JXDocument jxDocument, T t, String fieldName, String xpathMapping) throws Exception {
        List<Object> list = jxDocument.sel(xpathMapping);
        Integer integer = Integer.valueOf(list.get(0).toString());
        String methodName = HandlerUtils.getMethodName(fieldName);
        Method method = clazz.getDeclaredMethod(methodName, Integer.class);
        method.invoke(t, integer);
    }

    @Override
    public String getType() {
        return Integer.class.getName();
    }

}